package com.server.web.controller.officialAccounts;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.server.common.core.domain.AjaxResult;
import com.server.common.utils.StringUtils;
import com.server.pinggu.domain.SenPgy;
import com.server.pinggu.domain.SenUser;
import com.server.pinggu.domain.SenUserdata;
import com.server.pinggu.service.ISenPgyService;
import com.server.pinggu.service.ISenUserService;
import com.server.pinggu.service.ISenUserdataService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname FengRui
 * @Description 公众号相关接口
 * @Date 2022-11-15 14:05
 */
@RestController
@RequestMapping("/officialAccounts/user")
@AllArgsConstructor
public class SenUserInfoController {

    private final ISenUserService iSenUserService;

    private final ISenUserdataService iSenUserdataService;

    private final ISenPgyService iSenPgyService;


    @ApiOperation(value = "新增评估员信息")
    @PostMapping("add")
    public AjaxResult addUser(@RequestBody SenUser senUser){
        iSenUserService.addUser(senUser);
        return AjaxResult.success();
    }

    @ApiOperation(value = "判断用户权限")
    @GetMapping("determiningUserRights/{openId}")
    public AjaxResult determiningUserRights(@PathVariable("openId") String openId){
        if (StringUtils.isEmpty(openId)){
            throw new RuntimeException("openId can not be empty");
        }
        Map<String,Object> map = new HashMap<>();
        SenUser one = iSenUserService.getOne(new LambdaUpdateWrapper<SenUser>().eq(SenUser::getOopenid, openId));
        if (ObjectUtils.isNotEmpty(one)) {
            map.put("senUser", one);
            SenUserdata byId = iSenUserdataService.getById(one.getId());
            map.put("isPgy",false);
            map.put("isLuru",false);
            if (null != byId){
                map.put("senUserdata",byId);
                if (byId.getPgyid()!=null && byId.getPgyid()!=0){
                    SenPgy pgyId = iSenPgyService.getById(byId.getPgyid());
                    if (pgyId!=null && pgyId.getBangding() == 1){
                        map.put("pgyId",byId.getPgyid());
                        map.put("isPgy",true);
                    }else{
                        map.put("isPgy",false);
                    }
                }
                if (byId.getIsLuru()!=null&& byId.getIsLuru()!=0){
                    map.put("isLuru",true);
                }
                if (byId.getIsAdmin() == 1) {
                    map.put("isPgy",true);
                    map.put("isLuru",true);
                }
            }
        }else{
            map.put("senUser", null);
            map.put("senUserdata", null);
            map.put("pgyId",null);
            map.put("isPgy",false);
            map.put("isLuru",false);
        }
        return AjaxResult.success(map);
    }

    @ApiOperation(value = "根据openId获取评估员所有信息")
    @PostMapping("getUserInfoByOpenId/{openId}")
    public AjaxResult getUserInfoByOpenId(@PathVariable("openId") String openId){
        if (StringUtils.isEmpty(openId)){
            throw new RuntimeException("openId can not be empty");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("senUser",null);
        map.put("senUserdata",null);
        map.put("senPgy",null);
        map.put("streetList",null);
        SenUser one = iSenUserService.getOne(new LambdaQueryWrapper<SenUser>().eq(SenUser::getOopenid,openId));
        if (one!=null) {
            map.put("senUser",one);
            SenUserdata byId = iSenUserdataService.getById(one.getId());
            if (byId!=null &&byId.getPgyid()!=null){
                map.put("senUserdata",byId);
                SenPgy pgy = iSenPgyService.getById(byId.getPgyid());
                if (pgy!=null){
                    String juweiId = pgy.getJuweiId();
                    if (StringUtils.isNotEmpty(juweiId)){
                        map.put("senPgy",pgy);
                        map.put("streetList",iSenPgyService.selectJuWei(Arrays.asList(juweiId.split(","))));
                    }
                }
            }
        }
        return AjaxResult.success(map);
    }
}
